import { Application } from 'egg';
import { DataTypes, Model } from 'sequelize';

export interface TagAttributes {
  id?: number;
  name: string;
  type: 'food' | 'play';
  created_at?: Date;
  updated_at?: Date;
}

export interface TagInstance extends Model<TagAttributes>, TagAttributes {}

export default (app: Application) => {
  const { STRING, INTEGER, ENUM, DATE } = DataTypes;

  const Tag = app.model.define<TagInstance>('Tag', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    name: {
      type: STRING(50),
      allowNull: false,
      comment: '标签名称',
    },
    type: {
      type: ENUM('food', 'play'),
      allowNull: false,
      comment: '标签类型',
    },
    created_at: {
      type: DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    updated_at: {
      type: DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
  }, {
    tableName: 'tags',
    comment: '标签表',
  });

  return Tag;
};